//通用的数据处理方法

//array to map
const arrToMap = function(array, key) {
    if (!array) {
        return '';
    }
    var map = {};
    array.forEach(function(arr, index) {
        var value;
        if ( key !== undefined ) {
            value = fetchFromObject(arr, key);
        } else {
            value = arr;
        }
        if ( value !== undefined ) {
            map[value] = arr;
        } else {
            map[index] = arr;
        }
    });
    return map;
}

//map to array
const mapToArr = function(map) {
    var array = [];
    for (var key in map) {
        array.push(map[key]);
    }
    return array;
}

//fetchFromObject
const fetchFromObject = function(obj, prop) {
    if (typeof obj === 'undefined' || !obj) {
        return '--';
    }

    var _index = prop.indexOf('.');
    if (_index > -1) {
        return fetchFromObject(obj[prop.substring(0, _index)], prop.substr(_index + 1));
    }

    return obj[prop];
}

export {
    arrToMap,
    mapToArr,
    fetchFromObject
}
